package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.SysPtReportAddBo;
import com.ruoyi.system.bo.SysPtReportQueryBo;
import com.ruoyi.system.bo.SysPtReportEditBo;
import com.ruoyi.system.domain.SysPtReport;
import com.ruoyi.system.mapper.SysPtReportMapper;
import com.ruoyi.system.vo.SysPtReportVo;
import com.ruoyi.system.service.ISysPtReportService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 实验报告管理Service业务层处理
 *
 * @author 莫修
 * @date 2021-11-28
 */
@Service
public class SysPtReportServiceImpl extends ServiceImpl<SysPtReportMapper, SysPtReport> implements ISysPtReportService {

    @Override
    public SysPtReportVo queryById(Long reportId){
        return getVoById(reportId, SysPtReportVo.class);
    }

    @Override
    public TableDataInfo<SysPtReportVo> queryPageList(SysPtReportQueryBo bo) {
        PagePlus<SysPtReport, SysPtReportVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysPtReportVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysPtReportVo> queryList(SysPtReportQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysPtReportVo.class);
    }

    private LambdaQueryWrapper<SysPtReport> buildQueryWrapper(SysPtReportQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysPtReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExpId() != null, SysPtReport::getExpId, bo.getExpId());
        lqw.eq(bo.getUserId() != null, SysPtReport::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getSubmit()), SysPtReport::getSubmit, bo.getSubmit());
        lqw.eq(bo.getSubmitTime() != null, SysPtReport::getSubmitTime, bo.getSubmitTime());
        lqw.eq(StrUtil.isNotBlank(bo.getReview()), SysPtReport::getReview, bo.getReview());
        lqw.eq(bo.getReviewTime() != null, SysPtReport::getReviewTime, bo.getReviewTime());
        lqw.eq(bo.getPurpose() != null, SysPtReport::getPurpose, bo.getPurpose());
        lqw.eq(bo.getTool() != null, SysPtReport::getTool, bo.getTool());
        lqw.eq(bo.getPrinciple() != null, SysPtReport::getPrinciple, bo.getPrinciple());
        lqw.eq(bo.getContent() != null, SysPtReport::getContent, bo.getContent());
        lqw.eq(bo.getData() != null, SysPtReport::getData, bo.getData());
        lqw.eq(bo.getSum() != null, SysPtReport::getSum, bo.getSum());
        lqw.eq(bo.getScore() != null, SysPtReport::getScore, bo.getScore());
        lqw.eq(bo.getIsEnable() != null, SysPtReport::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysPtReportAddBo bo) {
        SysPtReport add = BeanUtil.toBean(bo, SysPtReport.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysPtReportEditBo bo) {
        SysPtReport update = BeanUtil.toBean(bo, SysPtReport.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysPtReport entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
